/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cronus.server;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;

/**
 *
 * @author Jannick
 */
public class MainScreen extends JFrame implements ActionListener {
    //Maak een nieuwe main aan
    private Main main;
    //String die de datum bevat
    private String dateTime;
    //Logscherm
    private String log = "";
    private JPanel panel;
    private JTextArea logtext;
    //Scrollpane
    private JScrollPane scrollpane;
    //Menu
    private JMenuBar menubar;
    private JMenu menuoptions ;
    private JMenuItem itemclear;
    private JMenuItem itemstart;
    private JMenuItem itemstop;
    private JMenuItem leeslog;
    //Lezen van logbestanden
    private JFileChooser fc = new JFileChooser("logs\\");
        
            
    
    public MainScreen( Main main ){
        this.main = main;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        panel = new JPanel();
        setSize(640, 480);
        
        logtext = new JTextArea(33, 55);
        logtext.setLineWrap(true);
        logtext.setEditable(false);
        
        menubar = new JMenuBar();
        menuoptions = new JMenu("Options");
        
        itemclear = new JMenuItem("Clear");
        itemstart = new JMenuItem("Start");
        itemstop = new JMenuItem("Stop");
        leeslog = new JMenuItem("Laad Log");
        
        scrollpane = new JScrollPane(logtext);        
        scrollpane.setPreferredSize(new Dimension(620, 410));
        
        panel.add(scrollpane, BorderLayout.CENTER);
        panel.add(menubar);
        menubar.add( menuoptions );
        menuoptions.add( itemclear );
        menuoptions.add( itemstart);
        menuoptions.add( itemstop );
        menuoptions.add( leeslog );
        
        itemclear.addActionListener(this);
        itemstart.addActionListener(this);
        itemstop.addActionListener(this);
        leeslog.addActionListener(this);
        
        setTitle("Info Server");
        setContentPane( panel );
        setJMenuBar( menubar );
    }
    
    
    public void addToLog(String add){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        this.dateTime = dateFormat.format(date);
        log = log += dateTime + " " + add + "\n";
        refreshLogTekst(log);
    }
    
    public void refreshLogTekst(String log){
        logtext.setText(log);
    }
    
    public void actionPerformed(ActionEvent e) {
        if( e.getSource() == itemclear ){
            logtext.setText("");
        }
        else if( e.getSource() == itemstart ){
            main.getSocketConnection().startConnection();
        }
        else if( e.getSource() == itemstop ){
            main.getSocketConnection().stopConnection();
        }  
        else if( e.getSource() == leeslog){
            int r = fc.showOpenDialog(null);
                if (r == JFileChooser.APPROVE_OPTION) {
                    
                File file = fc.getSelectedFile();
                
                String filename = file.toString();
                
                PopupScreen logpopup = new PopupScreen(filename);
                
                logpopup.setVisible(true);
        }
    }
}
}
